$(document).ready(function(){
	fnAddEventForAdmin();
	bFnIsLogin(fnIsLoginCallback);
});
function fnAddEventForAdmin(){
	$(document).on('click' , '.dialogLogin .confirm' , function(){
		var adminName = $(this).parent().children('.account').val();
		var password = $(this).parent().children('.password').val();
		fnAdminLogin(adminName , password , fnGetList);
	});
	$(document).on('click','.deleteUser',function(){
		var userId = $(this).attr('data-userId');
		fnDeleteUser(userId);
	});
	$(document).on('click','.videoStatus',function(){
		var videoId = $(this).attr('data-videoId');
		fnFreeze(videoId);
	});
	$(document).on('click','.deleteVideo',function(){
		var videoId = $(this).attr('data-videoId');
		fnDeleteVideo(videoId);
	});
	$(document).on('click','.updateDescription',function(){
		$('.dialogDescription').addClass('show').attr('data-videoId',$(this).attr('data-videoId'));
		$('.layer').addClass('show');
	});
	$(document).on('click','.dialogDescription .confirm',function(){
		var videoDescription = $('.dialogDescription .description').val();
		var videoId = $(this).parent().attr('data-videoId');
		fnUpdateDescription(videoId,videoDescription);
	});
	$(document).on('click','.dialogDescription .cancel',function(){
		oDialogHandle.fnRemoveAll();
	});
}
function bFnIsLogin(callback){
	$.ajax({
		url:'/api/admin.php',
		type:'post',
		dataType:'json',
		data:{
			method:'isLogin'
		},
		success:function(data){
			callback(data);
		}
	});
}

function fnIsLoginCallback(data){
	var root = {list : data};
	if(data.loginStatus){
		oDialogHandle.fnRemoveAll();
		fnRenderNav(data);
		fnGetUserList();
		fnGetVideoList();
	}
}
function fnAdminLogin(adminName,password,callback){
	$.ajax({
		url:'/api/admin.php',
		type:'post',
		dataType:'json',
		data:{
			method:'login',
			adminName:adminName,
			password:password
		},
		success:function(data){
			callback(data);
		}
	});
}
function fnGetList(data){
	console.log(data);
	if(data.status == 'login successfully'){
		oDialogHandle.fnRemoveAll();
		fnRenderNav(data);
		fnGetUserList();
		fnGetVideoList();
	}else{
		alert('用户名或密码错误');
	}
}
function fnRenderNav(data){
	var root = {list : data};
	var oRenderOption = {
		sTplId : 'tpl-personal',
		oData : root,
		sRenderType : 'html',
		oContainer : $('.header') 
	}
	fnRender(oRenderOption);
}
function fnGetUserList(){
	$.ajax({
		url:'/api/admin.php',
		type:'post',
		dataType:'json',
		data:{
			method:'userList'
		},
		success:function(data){
			console.log(data);
			fnRenderUserList(data);
		}
	});
}
function fnGetVideoList(){
	$.ajax({
		url:'/api/admin.php',
		type:'post',
		dataType:'json',
		data:{
			method:'videoList'
		},
		success:function(data){
			console.log(data);
			fnRenderVideoList(data);
		}
	});
}
function fnRenderUserList(data){
	var root = {list : data};
	var oRenderOption = {
		sTplId : 'tpl-userList',
		oData : root,
		sRenderType : 'html',
		oContainer : $('.userList') 
	}
	fnRender(oRenderOption);
}
function fnRenderVideoList(data){
	var root = {list : data};
	var oRenderOption = {
		sTplId : 'tpl-videoList',
		oData : root,
		sRenderType : 'html',
		oContainer : $('.videoList') 
	}
	fnRender(oRenderOption);
}
function fnDeleteUser(userId){
	$.ajax({
		url:'/api/admin.php',
		type:'post',
		dataType:'json',
		data:{
			method:'deleteUser',
			userId:userId
		},
		success:function(data){
			console.log(data);
			if(data.status == 'success'){
				fnGetUserList();
			}else{
				alert('操作失败，请刷新重试');
			}
		}
	});
}
function fnFreeze(videoId){
	$.ajax({
		url:'/api/admin.php',
		type:'post',
		dataType:'json',
		data:{
			method:'freeze',
			videoId:videoId
		},
		success:function(data){
		//	console.log(data);
			if(data.status == 'success'){
				fnGetVideoList();
			}else{
				alert('操作失败，请刷新重试');
			}
		}
	});	
}
function fnDeleteVideo(videoId){
	$.ajax({
		url:'/api/admin.php',
		type:'post',
		dataType:'json',
		data:{
			method:'deleteVideo',
			videoId:videoId
		},
		success:function(data){
		//	console.log(data);
			if(data.status == 'success'){
				fnGetVideoList();
			}else{
				alert('操作失败，请刷新重试');
			}
		}
	});	
}
function fnUpdateDescription(videoId,videoDescription){
	$.ajax({
		url:'/api/admin.php',
		type:'post',
		dataType:'json',
		data:{
			method:'updateDescription',
			videoId:videoId,
			videoDescription:videoDescription
		},
		success:function(data){
			console.log(data);
			if(data.status == 'success'){
				fnGetVideoList();
				oDialogHandle.fnRemoveAll();
			}else{
				alert('操作失败，请刷新重试');
			}
		}
	});	
}